<?php

function document_template_add_page() {
    return drupal_get_form('document_template_edit_form');
}

function document_template_edit_page($id) {
    return drupal_get_form('document_template_edit_form', $id);
}

function document_template_edit_form($form, &$form_state)
{
    $id = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : NULL;
    if (!empty($id)) {
        $record = template_load($id);
        if (empty($record)) {
            drupal_set_message(t('模板不存在！'), 'status', FALSE);
            drupal_goto('document_template/add');
        }
    }

    // 模板
    $form['template_id'] = array('#type' => 'hidden', '#default_value' => $id);
    $form['form_identifier'] = array(
        '#type' => 'textfield',
        '#title' => '表单ID',
        '#default_value' => !empty($record) ? $record->form_identifier : '',
        '#required' => TRUE,
        '#disabled' => !empty($record) ? TRUE : FALSE,
        '#maxlength' => 20,

    );
    $form['title'] = array(
        '#type' => 'textfield',
        '#title' => '表单名称',
        '#default_value' => !empty($record) ? $record->title : '',
        '#required' => TRUE,
        '#maxlength' => 20,
    );
    $form['form_json'] = array(
        '#type' => 'textarea',
        '#title' => '表单定义JSON字符串',
        '#default_value' => !empty($record) ? $record->form_json : '',
        '#required' => FALSE,
    );

    $form['template_submit'] = array (
        '#type' => 'submit',
        '#name' => 'template_submit',
        '#value' => '提交文档模板',
        );

    return $form;
}

function document_template_edit_form_validate($form, &$form_state)
{
    // todo , validate json string
}

function document_template_edit_form_submit($form, &$form_state)
{
    global $user;

    $template_id = $form_state['values']['template_id'];
    $form_identifier = $form_state['values']['form_identifier'];
    $title = $form_state['values']['title'];
    $uid = $user->uid;
    $form_json = $form_state['values']['form_json'];
    $timestamp = time();

    // treat alternation of old template as newly creation of template
    try {
//        $conn = _connection_mongodb();
//        $db = $conn->junong;
        $collection = $form_identifier.'_'.$timestamp;
//        $db->createCollection($collection);
//        $conn->close();

        db_insert('document_template')
            ->fields(array(
                'title' => $title,
                'form_identifier' => $form_identifier,
                'author_uid' => $uid,
                'collection' => $collection,
                'form_json' => $form_json,
                'timestamp' => $timestamp,
            ))
            ->execute();
    }
    catch (Exception $e) {
        drupal_set_message('数据库操作失败', 'status', FALSE);
    }

    if (empty($template_id)) {
        drupal_set_message('添加成功', 'status', FALSE);
    } else {
        drupal_set_message('更新成功', 'status', FALSE);
    }

    drupal_goto('document_template/list');
}

function template_load($id) {
    $result = db_select('document_template', 'M')
        -> fields('M')
        -> condition('M.document_template_id',$id, '=')
        -> execute()
        -> fetchObject();
    return $result;
}